import axios from 'axios'
import store from '@/store'

const request = axios.create({
  baseURL: 'http://hmmm-api.itheima.net',
  timeout: 5000
})

// 请求拦截器
request.interceptors.request.use((config) => {
  const { token } = store.state.user
  if (token) {
    // 通过请求头携带给后台，给headers添加Authorization
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

// 响应拦截器
request.interceptors.response.use((resp) => {
  return resp.data
}, error => Promise.reject(error))

export default request
